<?php
class ModelConfigMenu extends Model {
	public function addMenu($data) {
		//保存基本信息
		$this->db->query("INSERT INTO `" . DB_PREFIX . "config_menu` SET menu_name = '" . $this->db->escape($data['menu_name']) .  "', common_id = '" . $this->db->escape($data['common_id']) . "'");
		$menu_id = $this->db->getLastId();
		//保存菜单信息
		$this->addOrEditMenuRoute($menu_id,$data)  ;
	}
	
	public function editMenu($menu_id,$data) {
		//更新当前的基本信息
		$this->db->query("UPDATE `" . DB_PREFIX . "config_menu` SET menu_name = '" . $this->db->escape($data['menu_name']) ."', common_id = '" . $this->db->escape($data['common_id']) ."' WHERE menu_id = '" . (int)$menu_id . "'");
		//更新菜单信息
		$this->addOrEditMenuRoute($menu_id,$data)  ;
	}
	
	
	public function getAllMenuRoutes(){
		$Sql = "select * from `" . DB_PREFIX . "config_menu_route`  cmr left join ".DB_PREFIX."config_menu cm  on(cmr.menu_id=cm.menu_id) where parent_menu_id!=0 and menu_type='click'";   
		return $this->db->query($Sql)->rows;
	}
	
	
	public function getMenuRoute($menu_route_id){
		$Sql = "select * from `" . DB_PREFIX . "config_menu_route`  where menu_route_id='".(int)$menu_route_id."'";
		return $this->db->query($Sql)->row;
	}
	
	public function addOrEditMenuRoute($menu_id=0,$data=array()){
	 //删除当前基本信息下的所有菜单信息
		$delSql = "delete from `" . DB_PREFIX . "config_menu_route` where menu_id='".(int)$menu_id."'";   
		$this->db->query($delSql);
		$menus = $data['menu_route'];
		
		//保存菜单信息
		foreach ($menus as $menu) {
			$insertRouteSql = "insert into `" . DB_PREFIX . "config_menu_route` set menu_type= '".$this->db->escape($menu['type'])."', menu_url = '".$this->db->escape($menu['url'])."', display_name ='".$this->db->escape($menu['name'])."', sort_order ='".$this->db->escape($menu['sort_order'])."', menu_id='".(int)$menu_id."'";
			$this->db->query($insertRouteSql);
			$parent_id = $this->db->getLastId();
			$subs = $menu['sub'];
			foreach ($subs as $sub) {
				$insertSubRouteSql = "insert into `" . DB_PREFIX . "config_menu_route` set menu_type= '".$this->db->escape($sub['type'])."', menu_url = '".html_entity_decode($this->db->escape($sub['url'], ENT_QUOTES, 'UTF-8'))."', display_name ='".$this->db->escape($sub['name'])."', sort_order ='".$this->db->escape($sub['sort_order'])."', parent_menu_id ='".(int)$parent_id."', menu_id='".(int)$menu_id."'";
				$this->db->query($insertSubRouteSql);
			}
		}
	}
	
	
	public function getParentMenus($menu_id)
	{
		$sql = "select * from `" . DB_PREFIX . "config_menu_route` where menu_id='".(int)$menu_id."' and parent_menu_id=0 order by sort_order asc";
		return $this->db->query($sql)->rows;
	}
	
	public function getSubMenu($parent_menu_id)
	{
		$sql = "select * from `" . DB_PREFIX . "config_menu_route` where parent_menu_id='".(int)$parent_menu_id."' order by sort_order asc";
		return $this->db->query($sql)->rows;
	}
	
	
	public function updateStatus($menu_id,$menu_status)
	{
		$this->db->query("UPDATE `" . DB_PREFIX . "config_menu` SET  menu_status = '" . $menu_status ."' WHERE menu_id = '" . (int)$menu_id . "'");
	}
	
	public function getTotalMenus() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "config_menu`");
		return $query->row['total'];
	}
	
	public function getMenu($menu_id) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "config_menu` WHERE menu_id = '" . (int)$menu_id . "'");
		$baseInfo = $query->row;
		
		$baseInfo['menu_route'] = array();
		$parentSql = "select * from `" . DB_PREFIX . "config_menu_route` where menu_id ='".(int)$menu_id."' and parent_menu_id=0";
		$parents  = $this->db->query($parentSql)->rows;
		foreach ($parents as $parent) {
			$subSql = "select * from `" . DB_PREFIX . "config_menu_route` where menu_id ='".(int)$menu_id."' and parent_menu_id= '".$parent['menu_route_id']."'";
			$subs = $this->db->query($subSql)->rows;
			$subInfos = array();
			foreach($subs as $sub)
			{
				$subInfos[]=array(
					"type" => $sub["menu_type"],
					"name" => $sub["display_name"],
					"url" => $sub["menu_url"],
					"sort_order" => $sub["sort_order"],
				);
			}
			$baseInfo['menu_route'][] = array(
				"type" => $parent["menu_type"],
				"name" => $parent["display_name"],
				"url" => $parent["menu_url"],
				"sort_order" => $parent["sort_order"],
				"sub" => $subInfos,
			);
		}
		
		return $baseInfo;
	}
	
	public function deleteMenu($menu_id) {
		$this->db->query("DELETE FROM `" . DB_PREFIX . "config_menu` WHERE menu_id = '" . (int)$menu_id . "'");
	}
	
	public function getMenus($data = array()) {
		$sql = "SELECT * FROM `" . DB_PREFIX . "config_menu` where 1=1 ";
		
		$sort_data = array(
		'status',
			'date_added'
		);	
			
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
			
		$query = $this->db->query($sql);
	
		return $query->rows;
	}

	
}
?>